package com.vincent.suchao.util;

import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Transformation;

/**
 * absolute scale animation, only support absolute translate
 * 
 * @author idiottiger
 * @hide
 * 
 */

class AbsoluteScaleAnimation extends Animation {

	private float mFromXDelta;
	private float mToXDelta;
	private float mFromYDelta;
	private float mToYDelta;

	private float preTx = 1.0f, preTy = 1.0f, dx_cp, dy_cp, focus_x, focus_y;

	private Transformation mTransformation;

	/**
	 * 
	 * 
	 * @param fromXDelta
	 * @param toXDelta
	 * @param fromYDelta
	 * @param toYDelta
	 */
	public AbsoluteScaleAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {
		mTransformation = new Transformation();
		setScaleformation(fromXDelta, toXDelta, fromYDelta, toYDelta);
	}

	@Override
	protected void applyTransformation(float interpolatedTime, Transformation t) {
		float dx = 1;
		float dy = 1;
		if (mFromXDelta != mToXDelta) {
			dx = mFromXDelta + (mToXDelta - mFromXDelta) * interpolatedTime;
		}
		if (mFromYDelta != mToYDelta) {
			dy = mFromYDelta + (mToYDelta - mFromYDelta) * interpolatedTime;
		}
		dx_cp = dx;
		dy_cp = dy;
		t.getMatrix().setScale(dx_cp, dy_cp);
	}

	@Override
	public void initialize(int width, int height, int parentWidth, int parentHeight) {
		super.initialize(width, height, parentWidth, parentHeight);
	}

	/**
	 * set transformation, if you want repeat use this animation, you can {@link #reset()} and
	 * {@link #setTransformation(float, float, float, float)} again.
	 * 
	 * @param fromXDelta
	 * @param toXDelta
	 * @param fromYDelta
	 * @param toYDelta
	 */
	public void setScaleformation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {
		mFromXDelta = fromXDelta;
		mToXDelta = toXDelta;
		mFromYDelta = fromYDelta;
		mToYDelta = toYDelta;
		preTx = fromXDelta;
		preTy = fromYDelta;
	}

	public void reset() {
		super.reset();
		mFromXDelta = 0;
		mFromYDelta = 0;
		mToXDelta = 0;
		mToYDelta = 0;
		preTx = preTy = 1.0f;
		dx_cp = dy_cp = 0;
		mTransformation.clear();
	}

	/**
	 * get current post translate value
	 * 
	 * @param outVaules
	 */
	void getCurrentScale(float[] outVaules) {
		getTransformation(AnimationUtils.currentAnimationTimeMillis(), mTransformation);
		float tx = dx_cp / preTx;
		float ty = dy_cp / preTy;

		preTx = dx_cp;
		preTy = dy_cp;

		outVaules[0] = tx;
		outVaules[1] = ty;
	}

	public void setFocusPoint(float x, float y) {
		focus_x = x;
		focus_y = y;
	}

	public float getFocusX() {
		return focus_x;
	}

	public float getFocusY() {
		return focus_y;
	}

}